<?php

/**
 *栏目管理
 *@author huuwhois
 *@time 2017/11/17
*/

namespace app\admin\controller;

use app\admin\model\Category as category_model;
use think\Request;
use think\Db;

class Category extends Base
{
    protected $category_model;

    public function __construct(Request $request = null)
    {
        parent::__construct($request);

        $category_model = new category_model();
        $this->category_model = $category_model;
    }

    public function listing()
    {
   
        $data = $this->category_model->categoryChildren();
        
        $this->assign("data", $data);
        return $this->fetch('listing');
    }


    public function index()
    {
        return $this->listing();
    }

    public function add()
    {
        if (Request::instance()->isPOST()) {
            var_dump(input("post."));
            exit;

            // if(input('post.cate_name')=='' ){
            //     $this->error("栏目名不能为空");
            // }
            $this->category_model->category_name = input('post.cate_name');
            $this->category_model->pid =intval(input('post.pid'));
            $this->category_model->category_icon = ( null!==input('post.cate_icon') ) ? input('post.cate_icon'):"";
            $this->category_model->ctime = time();
            $this->category_model->sort = intval(input('post.sort'));
            $this->category_model->isshow = intval(input('post.isshow'));
            $this->category_model->desc = input('post.note');
    
            try {
                $this->category_model->save();
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                $msg = substr($msg, 0, 15);
                // echo $msg;exit;
                $this->error("错误代码:".$msg);
            }
                $this->success('添加成功', 'index');
        } else {
            $top = $this->get_second_pid();
            $this->assign('data', $top);
            return $this->fetch();
        }
    }

    public function del()
    {
        if (Request::instance()->isAjax()) {
            $id = input('post.id');
            $res = $this->category_model->destroy($id);
            if (!$res) {
                return ['status'=>0];
            } else {
                return ['status'=>1];
            }
        }
    }

    public function edit($id = "")
    {
        $data = $this->category_model->get($id);
        
        if (Request::instance()->isPOST()) {
            // var_dump(input('post.'));
            // var_dump($id);exit;
            if (input('post.cate_name')=='') {
                $this->error("栏目名不能为空");
            }
            $data->category_name = input('post.cate_name');
            $data->pid =intval(input('post.pid'));
            $data->category_icon = ( null!==input('post.cate_icon') ) ? input('post.cate_icon'):"";
            $data->ctime = time();
            $data->sort = intval(input('post.sort'));
            $data->isshow = intval(input('post.isshow'));
            $data->desc = input('post.note');
            try {
                $data->save();
            } catch (\Exception $e) {
                $msg = $e->getMessage();
                $msg = substr($msg, 0, 15);

                $this->error("错误提示:".$msg);
            }
            $this->success('编辑成功', 'index');
        } else {
            $top = $this->getSecondPid();
            $data = $data->toArray();
            $pidname = Db::table('dean_category')->where('id', $data['pid'])->field('id,category_name')->find();
            // var_dump($top);
            // var_dump($data);exit;
            $this->assign('top', $top);
            $this->assign('pidname', $pidname);
            $this->assign('data', $data);
            return $this->fetch();
        }
    }

    protected function getSecondPid()
    {
        $top = Db::table('dean_category')->where('pid', 0)->field('id,category_name')->select();
        // var_dump($top);exit;
        
        foreach ($top as &$pa) {
            $data = Db::table('dean_category')->where('pid', $pa['id'])->field('id,category_name')->select();
            $pa['child'] = $data;
        }
        return $top;
    }
}
